package com.honghu.villagepost.api

import android.text.TextUtils
import com.ada.common.util.GsonUtil
import com.honghu.villagepost.callback.DialogCallback
import com.honghu.villagepost.callback.LzyResponse
import com.honghu.villagepost.constant.Constants
import com.honghu.villagepost.entity.*
import com.honghu.villagepost.entity.request.LogisticsOrderReq
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.HttpParams
import org.json.JSONObject
import java.io.File
import java.util.*

/**
 * Created by mqwang on 2017/11/15.
 */
object ApiService : BaseApi() {


    /**
     * 文件上传
     */
    @JvmStatic
    fun uploader(tag: Any?, file: File,
                 callback: DialogCallback<LzyResponse<FileResult>>) {
        val fileList: MutableList<File> = ArrayList()
        fileList.add(file)
        OkGo.post<LzyResponse<FileResult>>(APIUrl.UPLOAD_FILE) //                .upJson(jsonObject)
                .tag(tag)
                .isMultipart(true)
                .addFileParams("file", fileList)
//                .upFile(file) //                .upFile(file)
                .execute(callback)
    }

    @JvmStatic
    fun goods_category(tag: Any?,
                       shopType: Int,
                       callback: DialogCallback<LzyResponse<List<Classify>>>?) {

        OkGo.get<LzyResponse<List<Classify>>>(APIUrl.GOODS_CATEGORY)
                .tag(tag)
                .params("shopType", shopType)
                .execute(callback)
    }

    /**
     * 商品列表
     */
    @JvmStatic
    fun goods(tag: Any?,
              shopId: Int,
              cid: Int,
              keywords: String,
              current: Int?,
              callback: DialogCallback<LzyResponse<Goods>>?) {

        var httpParams = HttpParams()
        if (shopId > 0) {
            httpParams.put("shopId", shopId)
        }
        if (cid > 0) {
            httpParams.put("cid", cid)
        }
        if (!TextUtils.isEmpty(keywords)) {
            httpParams.put("keywords", keywords)
        }
        httpParams.put("current", current!!)
        httpParams.put("size", Constants.PERPAGE)

        OkGo.get<LzyResponse<Goods>>(APIUrl.GOODS)
                .tag(tag)
                .params(httpParams)
                .execute(callback)
    }

    /**
     * 商品详情
     */
    @JvmStatic
    fun goods_id(tag: Any?,
                 goodsId: Int?,
                 callback: DialogCallback<LzyResponse<Goods>>?) {

        OkGo.get<LzyResponse<Goods>>(APIUrl.GOODS + "/" + goodsId)
                .tag(tag)
                .execute(callback)
    }

    /** 登录  */
    @JvmStatic
    fun login(tag: Any?,
              mobile: String?,
              smsCode: String?,
              openid: String,
              callback: DialogCallback<LzyResponse<Map<String, String>>>?) {
        val params: MutableMap<String?, Any?> = HashMap()
        params["mobile"] = mobile
        params["smsCode"] = smsCode

        if (!TextUtils.isEmpty(openid)) {
            params["openid"] = openid
        }
        val jsonObject = JSONObject(params)
        OkGo.post<LzyResponse<Map<String, String>>>(APIUrl.LOGIN)
                .tag(tag)
                .upJson(jsonObject)
                .execute(callback)
    }

    /** 发送验证码 */
    @JvmStatic
    fun verifycode(tag: Any?,
                   mobile: String?,
                   callback: DialogCallback<LzyResponse<Any>>?) {
        val params: MutableMap<String?, Any?> = HashMap()
        params["mobile"] = mobile

        val jsonObject = JSONObject(params)
        OkGo.post<LzyResponse<Any>>(APIUrl.SMS_CODE)
                .tag(tag)
                .upJson(jsonObject)
                .execute(callback)
    }

    /** 商家详情  */
    fun shop_id(tag: Any?,
                shopId: Int,
                callback: DialogCallback<LzyResponse<Store>>?) {

        OkGo.get<LzyResponse<Store>>(APIUrl.SHOP_ID + "/" + shopId)
                .tag(tag)
                .execute(callback)

    }

    /** 商家列表  */
    fun shop_list(tag: Any?,
                  shopType: Int,
                  current: Int,
                  callback: DialogCallback<LzyResponse<Store>>?) {

        if (shopType == 0) {
            OkGo.get<LzyResponse<Store>>(APIUrl.SHOP_ID)
                    .tag(tag)
                    .params("current", current!!)
                    .params("size", Constants.PERPAGE)
                    .execute(callback)
        } else {
            OkGo.get<LzyResponse<Store>>(APIUrl.SHOP_ID)
                    .tag(tag)
                    .params("shopType", shopType!!)
                    .params("current", current!!)
                    .params("size", Constants.PERPAGE)
                    .execute(callback)
        }
    }

    /** 商家列表  */
    fun shop_list(tag: Any?,
                  current: Int,
                  callback: DialogCallback<LzyResponse<Store>>?) {

        OkGo.get<LzyResponse<Store>>(APIUrl.SHOP_ID)
                .tag(tag)
                .params("current", current!!)
                .params("size", Constants.PERPAGE)
                .execute(callback)

    }

    /** 地址列表  */
    @JvmStatic
    fun user_address(tag: Any?,
                     keywords: String,
                     current: Int,
                     callback: DialogCallback<LzyResponse<Addr>>) {

        OkGo.get<LzyResponse<Addr>>(APIUrl.USER_ADDRESS)
                .tag(tag)
                .params("keywords", keywords)
                .params("current", current!!)
                .params("size", Constants.PERPAGE)
                .execute(callback)

    }


    /** 新增地址  */
    fun user_address(tag: Any?,
                     adcode: String?,
                     address: String?,
                     city: String?,
                     district: String?,
                     latitude: Double?,
                     longitude: Double?,
                     mobile: String?,
                     name: String?,
                     province: String?,
                     street: String?,
                     defaultAddr: Boolean,
                     callback: DialogCallback<LzyResponse<Any>>?) {
        val params: MutableMap<String?, Any?> = HashMap()
        params["adcode"] = adcode
        params["address"] = address
        params["city"] = city
        params["district"] = district
        params["latitude"] = latitude
        params["longitude"] = longitude
        params["mobile"] = mobile
        params["name"] = name
        params["province"] = province
        params["street"] = street
        params["defaultAddr"] = defaultAddr

        val jsonObject = JSONObject(params)
        OkGo.post<LzyResponse<Any>>(APIUrl.USER_ADDRESS)
                .tag(tag)
                .upJson(jsonObject)
                .execute(callback)
    }


    /** 修改地址  */
    fun user_address(tag: Any?,
                     adcode: String?,
                     id: Int?,
                     address: String?,
                     city: String?,
                     district: String?,
                     latitude: Double?,
                     longitude: Double?,
                     mobile: String?,
                     name: String?,
                     province: String?,
                     street: String?,
                     defaultAddr: Boolean,
                     callback: DialogCallback<LzyResponse<Any>>?) {
        val params: MutableMap<String?, Any?> = HashMap()
        params["id"] = id
        params["adcode"] = adcode
        params["address"] = address
        params["city"] = city
        params["district"] = district
        params["latitude"] = latitude
        params["longitude"] = longitude
        params["mobile"] = mobile
        params["name"] = name
        params["province"] = province
        params["street"] = street
        params["defaultAddr"] = defaultAddr

        val jsonObject = JSONObject(params)
        OkGo.put<LzyResponse<Any>>(APIUrl.USER_ADDRESS)
                .tag(tag)
                .upJson(jsonObject)
                .execute(callback)
    }

    /** 地址删除  */
    fun user_address_delete(tag: Any?,
                            id: Int?,
                            callback: DialogCallback<LzyResponse<Any>>?) {

        OkGo.delete<LzyResponse<Any>>(APIUrl.USER_ADDRESS + "/" + id)
                .tag(tag)
                .execute(callback)
    }


    /** 订单结算  */
    fun order_settle(tag: Any?,
                     addressId: Int?,
                     settleItemList: MutableList<MutableMap<String, Any>>?,
                     shopId: Int?,
                     callback: DialogCallback<LzyResponse<OrderDetails>>?) {
        val params: MutableMap<String?, Any?> = HashMap()
        if (addressId != null) {
            params["addressId"] = addressId
        }
        params["settleItemList"] = settleItemList
        params["shopId"] = shopId

        val jsonObject = JSONObject(params)
        OkGo.post<LzyResponse<OrderDetails>>(APIUrl.ORDER_SETTLE)
                .tag(tag)
                .upJson(jsonObject)
                .execute(callback)
    }

    /** 订单创建  */
    fun order_create(tag: Any?,
                     addressId: Int?,
                     buyerMemo: String?,
                     settleItemList: MutableList<MutableMap<String, Any>>?,
                     shopId: Int?,
                     callback: DialogCallback<LzyResponse<MutableMap<String, Any>>>?) {
        val params: MutableMap<String?, Any?> = HashMap()
        if (addressId != null) {
            params["addressId"] = addressId
        }
        params["buyerMemo"] = buyerMemo
        params["settleItemList"] = settleItemList
        params["shopId"] = shopId

        val jsonObject = JSONObject(params)
        OkGo.post<LzyResponse<MutableMap<String, Any>>>(APIUrl.ORDER_CREATE)
                .tag(tag)
                .upJson(jsonObject)
                .execute(callback)
    }

    /** 订单详情  */
    fun order_details(tag: Any?,
                      orderNo: String?,
                      callback: DialogCallback<LzyResponse<OrderDetails>>?) {

        OkGo.get<LzyResponse<OrderDetails>>(APIUrl.ORDER + "/" + orderNo)
                .tag(tag)
                .execute(callback)

    }

    /**
     * 订单列表
     */
    fun order(tag: Any?,
              type: Int,
              current: Int?,
              callback: DialogCallback<LzyResponse<OrderDetails>>?) {

        var httpParams = HttpParams()
        if (type != 0) {
            httpParams.put("state", type)
        }
        httpParams.put("current", current!!)
        httpParams.put("size", Constants.PERPAGE)

        OkGo.get<LzyResponse<OrderDetails>>(APIUrl.ORDER)
                .tag(tag)
                .params(httpParams)
                .execute(callback)
    }

    /** 用户信息  */
    @JvmStatic
    fun user_info(tag: Any?,
                  callback: DialogCallback<LzyResponse<User>>?) {

        OkGo.get<LzyResponse<User>>(APIUrl.USER)
                .tag(tag)
                .execute(callback)
    }


    /** 用户信息修改  */
    fun user_modify(tag: Any?,
                    avatar: String?,
                    nickname: String?,
                    mobile: String?,
                    sex: Int?,
                    callback: DialogCallback<LzyResponse<Any>>?) {

        val params: MutableMap<String?, Any?> = HashMap()

        params["avatar"] = avatar

        params["mobile"] = mobile
        params["nickname"] = nickname
        params["sex"] = sex

        val jsonObject = JSONObject(params)

        OkGo.put<LzyResponse<Any>>(APIUrl.USER)
                .tag(tag)
                .upJson(jsonObject)
                .execute(callback)
    }

    /**
     * 网点列表
     */
    fun logistics_location(tag: Any?,
                           latitude: Double,
                           longitude: Double,
                           callback: DialogCallback<LzyResponse<MutableList<Store>>>?) {

        var httpParams = HttpParams()
        httpParams.put("latitude", latitude)
        httpParams.put("longitude", longitude)

        OkGo.get<LzyResponse<MutableList<Store>>>(APIUrl.LOGISTICS_LOCATION)
                .tag(tag)
                .params(httpParams)
                .execute(callback)
    }

    /**
     * 寄件订单列表
     */
    fun logistics_order(tag: Any?,
                        current: Int?,
                        callback: DialogCallback<LzyResponse<Logistics>>?) {

        var httpParams = HttpParams()
        httpParams.put("current", current!!)
        httpParams.put("size", Constants.PERPAGE)

        OkGo.get<LzyResponse<Logistics>>(APIUrl.LOGISTICS_ORDER)
                .tag(tag)
                .params(httpParams)
                .execute(callback)
    }

    /**
     * 寄件订单详情
     */
    fun logistics_order_details(tag: Any?,
                                id: String?,
                                callback: DialogCallback<LzyResponse<Logistics>>?) {

        OkGo.get<LzyResponse<Logistics>>(APIUrl.LOGISTICS_ORDER + "/" + id)
                .tag(tag)
                .execute(callback)
    }

    /**
     * 物流运单号创建
     */
    @JvmStatic
    fun logistics_orde_create(tag: Any?,
                              logisticsOrder: LogisticsOrderReq,
                              callback: DialogCallback<LzyResponse<MutableMap<String, Any>>>?) {

        val params: MutableMap<String?, Any?> = HashMap()

        if (!TextUtils.isEmpty(logisticsOrder.goodsInfo)) {
            params["goodsInfo"] = logisticsOrder.goodsInfo
        }

        params["payType"] = logisticsOrder.payType
        params["pickUpEndTime"] = logisticsOrder.pickUpEndTime
        params["pickUpStartTime"] = logisticsOrder.pickUpStartTime
        params["type"] = logisticsOrder.type
        params["quantity"] = logisticsOrder.quantity
        params["userMemo"] = logisticsOrder.userMemo

        if (logisticsOrder.senderAddress != null) {
            val senderAddressMap = GsonUtil.parse(GsonUtil.toJson(logisticsOrder.senderAddress), Map::class.java)
            params["senderAddress"] = senderAddressMap
        }

        if (logisticsOrder.receiverAddress != null) {
            val receiverAddressMap = GsonUtil.parse(GsonUtil.toJson(logisticsOrder.receiverAddress), Map::class.java)
            params["receiverAddress"] = receiverAddressMap
        }

        params["weight"] = logisticsOrder!!.weight
        params["quantity"] = logisticsOrder!!.quantity

        val jsonObject = JSONObject(params)

        OkGo.post<LzyResponse<MutableMap<String, Any>>>(APIUrl.LOGISTICS_ORDER_CREATE)
                .tag(tag)
                .upJson(jsonObject)
                .execute(callback)
    }

    /**
     * 物流运单号结算
     */
    fun logistics_order_settle(tag: Any?,
                               logisticsOrder: LogisticsOrderReq?,
                               callback: DialogCallback<LzyResponse<LogisticsOrderReq>>?) {

        val params: MutableMap<String?, Any?> = HashMap()

        params["goodsInfo"] = logisticsOrder!!.goodsInfo
        params["payType"] = logisticsOrder!!.payType
        params["pickUpEndTime"] = logisticsOrder!!.pickUpEndTime
        params["pickUpStartTime"] = logisticsOrder!!.pickUpStartTime
        params["type"] = logisticsOrder!!.type

        val receiverAddressMap = GsonUtil.parse(GsonUtil.toJson(logisticsOrder!!.receiverAddress), Map::class.java)
        val senderAddressMap = GsonUtil.parse(GsonUtil.toJson(logisticsOrder!!.senderAddress), Map::class.java)

        params["receiverAddress"] = receiverAddressMap
        params["senderAddress"] = senderAddressMap

        params["weight"] = logisticsOrder!!.weight

        val jsonObject = JSONObject(params)

        OkGo.post<LzyResponse<LogisticsOrderReq>>(APIUrl.LOGISTICS_ORDER_SETTLE)
                .tag(tag)
                .upJson(jsonObject)
                .execute(callback)
    }


    /** 订单取消  */
    fun logistics_order_cancel(tag: Any?,
                               logisticsNo: String?,
                               callback: DialogCallback<LzyResponse<Any>>?) {
        val params: MutableMap<String?, Any?> = HashMap()
        params["logisticsNo"] = logisticsNo

        val jsonObject = JSONObject(params)
        OkGo.put<LzyResponse<Any>>(APIUrl.LOGISTICS_ORDER_CANCEL)
                .tag(tag)
                .upJson(jsonObject)
                .execute(callback)
    }


    /**
     * 微信支付
     */
    fun order_pay_wx(tag: Any?,
                     logisticsNo: String?,
                     callback: DialogCallback<LzyResponse<Map<String, String>>>?) {

        val params: MutableMap<String?, Any?> = HashMap()
        params["orderNo"] = logisticsNo

        val jsonObject = JSONObject(params)
        OkGo.post<LzyResponse<Map<String, String>>>(APIUrl.ORDER_PAY_WX)
                .tag(tag)
                .upJson(jsonObject)
                .execute(callback)
    }

    /**
     * 微信支付
     */
    fun logistics_order_pay_wx(tag: Any?,
                               logisticsNo: String?,
                               callback: DialogCallback<LzyResponse<Map<String, String>>>?) {

        val params: MutableMap<String?, Any?> = HashMap()
        params["logisticsNo"] = logisticsNo

        val jsonObject = JSONObject(params)
        OkGo.post<LzyResponse<Map<String, String>>>(APIUrl.LOGISTICS_ORDER_PAY_WX)
                .tag(tag)
                .upJson(jsonObject)
                .execute(callback)
    }

    /**
     * 商品列表
     */
    @JvmStatic
    fun homeGoodList(tag: Any?,
                     callback: DialogCallback<LzyResponse<HomeGoodsList>>?) {
        OkGo.get<LzyResponse<HomeGoodsList>>(APIUrl.HOME)
                .tag(tag)
                .execute(callback)
    }

    /**
     * 商品列表
     */
    @JvmStatic
    fun getGoodsLike(tag: Any?,
                     shopType: Int,
                     current: Int,
                     callback: DialogCallback<LzyResponse<HomeGoodsList>>?) {
        if (shopType == 0){
            OkGo.get<LzyResponse<HomeGoodsList>>(APIUrl.HOME)
                    .tag(tag)
                    .execute(callback)
        }else{
            OkGo.get<LzyResponse<HomeGoodsList>>(APIUrl.GOODS_LIKE)
                    .tag(tag)
                    .params("shopType", shopType)
                    .params("current", current)
                    .params("size", Constants.PERPAGE)
                    .execute(callback)
        }
    }

    /**
     * 商品列表
     */
    @JvmStatic
    fun weixLogin(tag: Any?,
                  code: String,
                  callback: DialogCallback<LzyResponse<WeixToken>>?) {
        val params: MutableMap<String?, Any?> = HashMap()
        params["code"] = code
        val jsonObject = JSONObject(params)
        OkGo.post<LzyResponse<WeixToken>>(APIUrl.WEIX_LOGIN)
                .tag(tag)
                .upJson(jsonObject)
                .execute(callback)
    }

    /**
     * 店铺申请
     */
    @JvmStatic
    fun applyShop(tag: Any?,
                  name: String,
                  idCardNo: String,
                  mobile: String,
                  idCardPicFront: String,
                  idCardPicBack: String,
                  callback: DialogCallback<LzyResponse<Any>>?) {

        val params: MutableMap<String?, Any?> = HashMap()
        params["name"] = name
        params["idCardNo"] = idCardNo
        params["mobile"] = mobile
        params["idCardPicFront"] = idCardPicFront
        params["idCardPicBack"] = idCardPicBack

        val jsonObject = JSONObject(params)
        OkGo.post<LzyResponse<Any>>(APIUrl.APPLY_SHOP)
                .tag(tag)
                .upJson(jsonObject)
                .execute(callback)
    }

    /** 修改商铺详情  */
    @JvmStatic
    fun modifyApplyShop(tag: Any?,
                        name: String,
                        idCardNo: String,
                        mobile: String,
                        idCardPicFront: String,
                        idCardPicBack: String,
                        callback: DialogCallback<LzyResponse<Any>>?) {
        val params: MutableMap<String?, Any?> = HashMap()
        params["name"] = name
        params["idCardNo"] = idCardNo
        params["mobile"] = mobile
        params["idCardPicFront"] = idCardPicFront
        params["idCardPicBack"] = idCardPicBack

        val jsonObject = JSONObject(params)
        OkGo.put<LzyResponse<Any>>(APIUrl.APPLY_SHOP)
                .tag(tag)
                .upJson(jsonObject)
                .execute(callback)
    }

    /**
     * 店铺详情
     */
    @JvmStatic
    fun getApplyDetail(tag: Any?,
                       callback: DialogCallback<LzyResponse<ShopApplyDetail>>?) {
        OkGo.get<LzyResponse<ShopApplyDetail>>(APIUrl.APPLY_SHOP)
                .tag(tag)
                .execute(callback)
    }

    /**
     * 获取版本信息
     */
    @JvmStatic
    fun updateVersion(tag: Any?,
                      callback: DialogCallback<LzyResponse<Version>>) {
        OkGo.get<LzyResponse<Version>>(APIUrl.LAUNCH + "/2")
                .tag(tag)
                .execute(callback)
    }
}